{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver import Chrome\n",
    "import time\n",
    "import csv\n",
    "import re\n",
    "        \n",
    "\n",
    "    \n",
    "\n",
    "def search_product(key):\n",
    "    web.find_element_by_id('q').send_keys(key)\n",
    "    web.find_element_by_class_name('btn-search').click()\n",
    "    web.maximize_window()\n",
    "    time.sleep(15)\n",
    "    page=web.find_element_by_class_name(\"total\").text\n",
    "    page=re.findall('(\\d+)',page)[0]\n",
    "    return int(page)\n",
    "def get_priduct():\n",
    "    divs=web.find_elements_by_xpath('//div[@class=\"items\"]/div[@class=\"item J_MouserOnverReq  \"]')\n",
    "    \n",
    "    for div in divs:\n",
    "        \n",
    "        \n",
    "        info=div.find_element_by_xpath('.//div[@class=\"row row-2 title\"]/a').text\n",
    "        price=div.find_element_by_xpath('.//strong').text+\"元\"\n",
    "        deal=div.find_element_by_xpath('.//div[@class=\"deal-cnt\"]').text\n",
    "        name=div.find_element_by_xpath('.//div[@class=\"shop\"]/a').text\n",
    "        #print(info,price,deal,name)\n",
    "        with open('python.csv','a',newline=\"\",encoding='utf-8') as file:\n",
    "            \n",
    "            res=csv.writer(file,delimiter=',')\n",
    "            res.writerow([info,price,deal,name])\n",
    "\n",
    "\n",
    "\n",
    "def main():\n",
    "    print('爬取第1页')\n",
    "    print('==========================================')\n",
    "    page=search_product(key)\n",
    "    res=get_priduct()\n",
    "    number=1\n",
    "    \n",
    "    while number!=5:\n",
    "        print('*'*100)\n",
    "        print('爬取第{}页'.format(number+1))\n",
    "        print('*'*100)\n",
    "        web.get('https://s.taobao.com/search?q={}&s={}'.format(key,number*44))\n",
    "        #web.maximize_window()\n",
    "        web.implicitly_wait(2)\n",
    "        get_priduct()\n",
    "        number+=1\n",
    "    \n",
    "\n",
    "if __name__=='__main__':\n",
    "    web=Chrome()\n",
    "    key=input('请输入数据：')\n",
    "    web.get('https://www.taobao.com/')\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "ok\n"
     ]
    }
   ],
   "source": [
    "d=dict()\n",
    "print(d)\n",
    "data={'信息':None,'价格':None,'付款人数':None,'店铺':None}\n",
    "if data['信息']==None:\n",
    "    print('ok')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import csv \n",
    "# with open('python.csv','r',encoding='utf-8') as f:\n",
    "#     res=csv.reader(f)\n",
    "#     for res in res:\n",
    "#         print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
